import React, { Component } from 'react'
import moment from 'moment'
import './index.less'

export default class Home extends Component {
  state = {
    curTime: ''
  }

  componentDidMount() {
    console.log('进入页面')
    this.getCurTime()
  }

  componentWillUnmount() {
    console.log('离开页面前')
    this.timer && clearInterval(this.timer)
  }

  getCurTime = () => {
    const getCurrentTime = () => {
      const curTime = moment().format('YYYY-MM-DD HH:mm:ss')
      this.setState({ curTime })
    }
    getCurrentTime()
    this.timer = setInterval(() => {
      getCurrentTime()
    }, 1000)
  }

  render() {
    const { curTime } = this.state
    return <div className="home">
      <div className="home-title">Welcome, Home</div>
      <div className="home-cur-time">当前时间：{curTime}</div>
    </div>
  }
}
